package com.platform.core.web.controller;

import com.platform.aspect.WebLog;
import com.platform.core.service.SysRoleFunctionService;
import com.platform.dto.FunctionQo;
import com.platform.comm.web.results.JsonResult;
import com.platform.comm.web.results.PageListJsonResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.data.domain.Pageable;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description: 授权管理
 *
 * @author libin Created on 2019/8/13
 **/
@RestController
@RequestMapping("management/role/function")
public class SysRoleFunctionController {
    private final SysRoleFunctionService sysRoleFunctionService;

    public SysRoleFunctionController(SysRoleFunctionService sysRoleFunctionService) {
        this.sysRoleFunctionService = sysRoleFunctionService;
    }

    @WebLog(theme = "添加[授权]", moduleName = "角色管理", type = WebLog.OpType.ADD, root = Integer.class, params = {"roleId", "functionId"})
    @PostMapping(name = "添加授权", path = "create")
    @Deprecated
    public JsonResult create(@RequestBody Map<String, Object> parametersMap) {
        String roleId = Objects.toString(parametersMap.get("roleId"), Strings.EMPTY);
        String functionId = Objects.toString(parametersMap.get("functionId"), Strings.EMPTY);
        sysRoleFunctionService.createRelated(roleId, functionId);
        return JsonResult.of();
    }

    @WebLog(theme = "添加[授权]", moduleName = "角色管理", type = WebLog.OpType.ADD, root = Integer.class, params = {"roleId", "functionIds"})
    @PostMapping(name = "批量添加授权", path = "batchCreate")
    public JsonResult batchCreate(@RequestBody Map<String, Object> parametersMap) {
        String roleId = Objects.toString(parametersMap.get("roleId"), Strings.EMPTY);
        List<String> functionIds = (List<String>) parametersMap.get("functionIds");

        sysRoleFunctionService.createRelated(roleId, functionIds);
        return JsonResult.of();
    }

    @WebLog(theme = "授权", moduleName = "角色管理", type = WebLog.OpType.ADD, root = Integer.class, params = {"roleId", "functionIds"})
    @PostMapping(name = "授权", path = "grant")
    @Deprecated
    public JsonResult batchAuthorize(@RequestBody Map<String, Object> parametersMap) {
        String roleId = Objects.toString(parametersMap.get("roleId"), Strings.EMPTY);
        List<String> functionIds = (List<String>) parametersMap.get("functionIds");

        sysRoleFunctionService.createOrRemoveRelated(roleId, functionIds);
        return JsonResult.of();
    }

    @WebLog(theme = "删除[授权]", moduleName = "角色管理", type = WebLog.OpType.DEL, root = Integer.class, params = {"roleId", "functionId"})
    @PostMapping(name = "删除授权", path = "remove")
    @Deprecated
    public JsonResult remove(@RequestBody Map<String, Object> parametersMap) {
        String roleId = Objects.toString(parametersMap.get("roleId"), Strings.EMPTY);
        String functionId = Objects.toString(parametersMap.get("functionId"), Strings.EMPTY);
        sysRoleFunctionService.removeRelated(roleId, functionId);
        return JsonResult.of();
    }

    @WebLog(theme = "删除[授权]", moduleName = "角色管理", type = WebLog.OpType.DEL, root = Integer.class, params = {"roleId", "functionIds"})
    @PostMapping(name = "批量删除授权", path = "batchRemove")
    @Deprecated
    public JsonResult batchRemove(@RequestBody Map<String, Object> parametersMap) {
        String roleId = Objects.toString(parametersMap.get("roleId"), Strings.EMPTY);
        List<String> functionIds = (List<String>) parametersMap.get("functionIds");

        sysRoleFunctionService.removeRelated(roleId, functionIds);
        return JsonResult.of();
    }

    @PostMapping(name = "分页查询权限列表", path = "queryFunctions")
    @Deprecated
    public PageListJsonResult<FunctionQo> queryFunctions(Pageable pageable, @RequestParam MultiValueMap<String, String> parameters) {
        return new PageListJsonResult<>(sysRoleFunctionService.queryFunctions(pageable, parameters));
    }
}
